package Demo3;

import java.util.*;

    public class Main3 {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int len = in.nextInt();
            String str1 = in.next();
            String str2 = in.next();
            int ret = Integer.MAX_VALUE;
            String[] strs = new String[len];
            for(int i = 0; i < len; i++) {
                strs[i] = in.nextLine();
            }
            int left = 0, right = 0;
            int index1 = -1, index2 = -1;
            while(left < len && right < len) {
                while(left < len && !strs[left].equals(str1)) {
                    index1 = left;
                    left++;
                }
                while(++right < len && !strs[right].equals(str2)) {
                    index2 = right;
                    right++;
                }
                if(index1 > index2) {
                    if(index2 != Integer.MAX_VALUE)ret = Math.min(index1 - index2,ret);
                }else {
                    if(index1 != Integer.MAX_VALUE)ret = Math.min(index2 - index1,ret);
                }
            }
            System.out.println(ret == Integer.MAX_VALUE ? -1 : ret);
        }
    }